package cn.jsp.spring;

import java.io.*;

import cn.jsp.spring.dao.StuDao;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
  private String message;

  public void init() {
    message = "Hello World!";
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    request.getSession().setAttribute("user","hello");

    // Hello
    PrintWriter out = response.getWriter();
    ApplicationContext ctx=WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
    StuDao dao=ctx.getBean(StuDao.class);
    out.println(dao.query());
  }

  public void destroy() {
  }
}